package cn.wolfcode.rbac.controller;


import cn.wolfcode.rbac.annotation.RequiredPermission;
import cn.wolfcode.rbac.config.PageInfo;
import cn.wolfcode.rbac.config.QueryObject;
import cn.wolfcode.rbac.model.RoleDO;
import cn.wolfcode.rbac.request.RoleRequest;
import cn.wolfcode.rbac.service.PermissionService;
import cn.wolfcode.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @author Lu.F
 * @version 1.0
 * @Description TODO
 * @date 2022/5/21 11:47
 */
@Controller
@RequestMapping("/role")
public class RoleController {


    @Autowired
    private RoleService roleService;

    @Autowired
    private PermissionService permissionService;

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @RequestMapping("list")
    @RequiredPermission({"角色列表", "role:list"})
    public String selectAll(Model model, QueryObject queryObject) {
//        List<DepartmentDO> departmentDOList = departmentService.selectAll();
//        model.addAttribute("departments",departmentDOList);
        PageInfo<RoleDO> pageInfo = roleService.pageDepartment(queryObject);
        model.addAttribute("pageInfo", pageInfo);
        return "role/list";
    }

    //修改input方法,回显部门信息
    @RequestMapping("/input")
    @RequiredPermission({"角色编辑", "role:input"})
    public String input(Model model, Long id) {
        //编辑/添加操作的角色回显数据
        if(id != null) {
            model.addAttribute("role", roleService.get(id));
        }
        model.addAttribute("permissions", permissionService.selectAll());
        return "role/input";
    }



    //添加保存/编辑方法
    @RequestMapping("/saveOrUpdate")
    @RequiredPermission({"角色保存或更新","role:saveOrUpdate"})
    public String saveOrUpdate(RoleRequest roleRequest){
        roleService.saveOrUpdate(roleRequest, roleRequest.getPermissionIds());
        return "redirect:/role/list";
    }


    @RequestMapping("/delete")
    @RequiredPermission({"角色删除","role:delete"})
    public String delete(Long id){
        roleService.delete(id);
        return "redirect:/role/list";
    }


}
